package com.dwd.pet.controller;

import com.dwd.basic.utils.AjaxResult;
import com.dwd.basic.utils.PageBean;
import com.dwd.basic.utils.SystemContext;
import com.dwd.org.mapper.ShopMapper;
import com.dwd.pet.mapper.PetDetailMapper;
import com.dwd.pet.pojo.Pet;
import com.dwd.pet.pojo.PetDetail;
import com.dwd.pet.pojo.SearchMasterMsg;
import com.dwd.pet.query.PetQuery;
import com.dwd.pet.query.SearchMasterMsgQuery;
import com.dwd.pet.service.ISearchMasterMsgService;
import com.dwd.pet.service.IPetService;
import com.dwd.user.pojo.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/24 12:35
 * @Version 1.0
 */

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private IPetService petService;


    /**
     * 门户网站发布消息
     *
     * @param searchMasterMsg
     * @param httpRequest
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest httpRequest) {
        try {
            seachMasterMsgService.publish(searchMasterMsg, httpRequest);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 根据shopid查询待处理寻主信息,分页
     *
     * @param searchMasterMsgQuery
     * @param httpRequest
     * @return
     */
    @PostMapping("/pending")
    public PageBean<SearchMasterMsg> queryPagePending(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest httpRequest) {
        //得到当前登陆用户
        LoginInfo loginInfo = SystemContext.getLoginInfo(httpRequest);
        //根据logininfo查询当前用户是否有shopId，如果有就是店铺管理员或员工,没有就是管理员
        Long shopId = shopMapper.queryShopIdByLoginInfo(loginInfo);
        if (shopId != null) {
            searchMasterMsgQuery.setShopId(shopId);
        }
        return seachMasterMsgService.queryPage(searchMasterMsgQuery);
    }

    /**
     * 处理寻主信息,存储到pet和petdetail
     *
     * @param pet
     * @return
     */
    @PostMapping("/savePet")
    public AjaxResult savePet(@RequestBody Pet pet, HttpServletRequest httpServletRequest) {
        try {
            petService.savePet(pet, httpServletRequest);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 已处理
     *
     * @param searchMasterMsgQuery
     * @param httpRequest
     * @return
     */
    @PostMapping("/process")
    public PageBean<SearchMasterMsg> queryPageProcess(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest httpRequest) {
        //得到当前登陆用户
        LoginInfo loginInfo = SystemContext.getLoginInfo(httpRequest);
        //根据logininfo查询当前用户是否有shopId，如果有就是店铺管理员或员工,没有就是管理员
        Long shopId = shopMapper.queryShopIdByLoginInfo(loginInfo);
        if (shopId != null) {
            searchMasterMsgQuery.setShopId(shopId);
        }
        return seachMasterMsgService.queryPage(searchMasterMsgQuery);
    }


}
